home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 14 / CU Amiga Magazine's Super CD-ROM 14 (1997)(EMAP Images)(GB)(Track 1 of 3)[!][issue 1997-09].iso / CUCD / Programming / Mesa-2.2 / src / feedback.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-03-13  |  10.0 KB  |  413 lines

  1. /* $Id: feedback.c,v 1.8 1997/03/08 02:03:46 brianp Exp $ */
  2.  
  3. /*
  4.  * Mesa 3-D graphics library
  5.  * Version:  2.2
  6.  * Copyright (C) 1995-1997  Brian Paul
  7.  *
  8.  * This library is free software; you can redistribute it and/or
  9.  * modify it under the terms of the GNU Library General Public
  10.  * License as published by the Free Software Foundation; either
  11.  * version 2 of the License, or (at your option) any later version.
  12.  *
  13.  * This library is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16.  * Library General Public License for more details.
  17.  *
  18.  * You should have received a copy of the GNU Library General Public
  19.  * License along with this library; if not, write to the Free
  20.  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  */
  22.  
  23.  
  24. /*
  25.  * $Log: feedback.c,v $
  26.  * Revision 1.8  1997/03/08 02:03:46  brianp
  27.  * better error checking
  28.  *
  29.  * Revision 1.7  1997/01/21 03:09:15  brianp
  30.  * fixed an error message in gl_RenderMode()
  31.  *
  32.  * Revision 1.6  1997/01/16 19:10:24  brianp
  33.  * kludged around a SunOS 5.x/GCC bug in write_hit_record()
  34.  *
  35.  * Revision 1.5  1996/12/13 21:03:19  brianp
  36.  * fixed feedback buffer overflow bug in write_hit_record()
  37.  *
  38.  * Revision 1.4  1996/10/22 22:58:25  brianp
  39.  * fixed bug in gl_update_hitflag() reported by Erich Eder
  40.  *
  41.  * Revision 1.3  1996/09/27 01:28:13  brianp
  42.  * added missing error check to gl_RenderMode()
  43.  *
  44.  * Revision 1.2  1996/09/15 14:17:30  brianp
  45.  * now use GLframebuffer and GLvisual
  46.  *
  47.  * Revision 1.1  1996/09/13 01:38:16  brianp
  48.  * Initial revision
  49.  *
  50.  */
  51.  
  52.  
  53. #include <assert.h>
  54. #include "context.h"
  55. #include "feedback.h"
  56. #include "dlist.h"
  57. #include "macros.h"
  58. #include "types.h"
  59.  
  60.  
  61.  
  62.  
  63. #define FB_3D        0x01
  64. #define FB_4D        0x02
  65. #define FB_INDEX    0x04
  66. #define FB_COLOR    0x08
  67. #define FB_TEXTURE    0X10
  68.  
  69.  
  70.  
  71. void
  72. gl_FeedbackBuffer( GLcontext *ctx, GLsizei size, GLenum type, GLfloat *buffer )
  73. {
  74.    if (ctx->RenderMode==GL_FEEDBACK || INSIDE_BEGIN_END(ctx)) {
  75.       gl_error( ctx, GL_INVALID_OPERATION, "glFeedbackBuffer" );
  76.       return;
  77.    }
  78.  
  79.    if (size<0) {
  80.       gl_error( ctx, GL_INVALID_VALUE, "glFeedbackBuffer(size<0)" );
  81.       return;
  82.    }
  83.    if (!buffer) {
  84.       gl_error( ctx, GL_INVALID_VALUE, "glFeedbackBuffer(buffer==NULL)" );
  85.       ctx->Feedback.BufferSize = 0;
  86.       return;
  87.    }
  88.  
  89.    switch (type) {
  90.       case GL_2D:
  91.      ctx->Feedback.Mask = 0;
  92.          ctx->Feedback.Type = type;
  93.      break;
  94.       case GL_3D:
  95.      ctx->Feedback.Mask = FB_3D;
  96.          ctx->Feedback.Type = type;
  97.      break;
  98.       case GL_3D_COLOR:
  99.      ctx->Feedback.Mask = FB_3D
  100.                            | (ctx->Visual->RGBAflag ? FB_COLOR : FB_INDEX);
  101.          ctx->Feedback.Type = type;
  102.      break;
  103.       case GL_3D_COLOR_TEXTURE:
  104.      ctx->Feedback.Mask = FB_3D
  105.                            | (ctx->Visual->RGBAflag ? FB_COLOR : FB_INDEX)
  106.                        | FB_TEXTURE;
  107.          ctx->Feedback.Type = type;
  108.      break;
  109.       case GL_4D_COLOR_TEXTURE:
  110.      ctx->Feedback.Mask = FB_3D | FB_4D
  111.                            | (ctx->Visual->RGBAflag ? FB_COLOR : FB_INDEX)
  112.                        | FB_TEXTURE;
  113.          ctx->Feedback.Type = type;
  114.      break;
  115.       default:
  116.      ctx->Feedback.Mask = 0;
  117.          gl_error( ctx, GL_INVALID_ENUM, "glFeedbackBuffer" );
  118.    }
  119.  
  120.    ctx->Feedback.BufferSize = size;
  121.    ctx->Feedback.Buffer = buffer;
  122.    ctx->Feedback.Count = 0;
  123. }
  124.  
  125.  
  126.  
  127. void gl_PassThrough( GLcontext *ctx, GLfloat token )
  128. {
  129.    if (INSIDE_BEGIN_END(ctx)) {
  130.       gl_error( ctx, GL_INVALID_OPERATION, "glPassThrough" );
  131.       return;
  132.    }
  133.  
  134.    if (ctx->RenderMode==GL_FEEDBACK) {
  135.       FEEDBACK_TOKEN( ctx, (GLfloat) GL_PASS_THROUGH_TOKEN );
  136.       FEEDBACK_TOKEN( ctx, token );
  137.    }
  138. }
  139.  
  140.  
  141.  
  142. /*
  143.  * Put a vertex into the feedback buffer.
  144.  */
  145. void gl_feedback_vertex( GLcontext *ctx,
  146.                          GLfloat x, GLfloat y, GLfloat z, GLfloat w,
  147.              const GLfloat color[4], GLfloat index,
  148.              const GLfloat texcoord[4] )
  149. {
  150.    FEEDBACK_TOKEN( ctx, x );
  151.    FEEDBACK_TOKEN( ctx, y );
  152.    if (ctx->Feedback.Mask & FB_3D) {
  153.       FEEDBACK_TOKEN( ctx, z );
  154.    }
  155.    if (ctx->Feedback.Mask & FB_4D) {
  156.       FEEDBACK_TOKEN( ctx, w );
  157.    }
  158.    if (ctx->Feedback.Mask & FB_INDEX) {
  159.       FEEDBACK_TOKEN( ctx, index );
  160.    }
  161.    if (ctx->Feedback.Mask & FB_COLOR) {
  162.       FEEDBACK_TOKEN( ctx, color[0] );
  163.       FEEDBACK_TOKEN( ctx, color[1] );
  164.       FEEDBACK_TOKEN( ctx, color[2] );
  165.       FEEDBACK_TOKEN( ctx, color[3] );
  166.    }
  167.    if (ctx->Feedback.Mask & FB_TEXTURE) {
  168.       FEEDBACK_TOKEN( ctx, texcoord[0] );
  169.       FEEDBACK_TOKEN( ctx, texcoord[1] );
  170.       FEEDBACK_TOKEN( ctx, texcoord[2] );
  171.       FEEDBACK_TOKEN( ctx, texcoord[3] );
  172.    }
  173. }
  174.  
  175.  
  176.  
  177. /**********************************************************************/
  178. /*                              Selection                             */
  179. /**********************************************************************/
  180.  
  181.  
  182. /*
  183.  * NOTE: this function can't be put in a display list.
  184.  */
  185. void gl_SelectBuffer( GLcontext *ctx, GLsizei size, GLuint *buffer )
  186. {
  187.    if (INSIDE_BEGIN_END(ctx)) {
  188.       gl_error( ctx, GL_INVALID_OPERATION, "glSelectBuffer" );
  189.    }
  190.    if (ctx->RenderMode==GL_SELECT) {
  191.       gl_error( ctx, GL_INVALID_OPERATION, "glSelectBuffer" );
  192.    }
  193.    ctx->Select.Buffer = buffer;
  194.    ctx->Select.BufferSize = size;
  195.    ctx->Select.BufferCount = 0;
  196.  
  197.    ctx->Select.HitFlag = GL_FALSE;
  198.    ctx->Select.HitMinZ = 1.0;
  199.    ctx->Select.HitMaxZ = 0.0;
  200. }
  201.  
  202.  
  203. void gl_InitNames( GLcontext *ctx )
  204. {
  205.    if (INSIDE_BEGIN_END(ctx)) {
  206.       gl_error( ctx, GL_INVALID_OPERATION, "glInitNames" );
  207.    }
  208.    ctx->Select.NameStackDepth = 0;
  209.    ctx->Select.HitFlag = GL_FALSE;
  210.    ctx->Select.HitMinZ = 1.0;
  211.    ctx->Select.HitMaxZ = 0.0;
  212. }
  213.  
  214.  
  215.  
  216. #define WRITE_RECORD( CTX, V )                    \
  217.     if (CTX->Select.BufferCount < CTX->Select.BufferSize) {    \
  218.        CTX->Select.Buffer[CTX->Select.BufferCount] = (V);        \
  219.     }                            \
  220.     CTX->Select.BufferCount++;
  221.  
  222.  
  223.  
  224. void gl_update_hitflag( GLcontext *ctx, GLfloat z )
  225. {
  226.    ctx->Select.HitFlag = GL_TRUE;
  227.    if (z < ctx->Select.HitMinZ) {
  228.       ctx->Select.HitMinZ = z;
  229.    }
  230.    if (z > ctx->Select.HitMaxZ) {
  231.       ctx->Select.HitMaxZ = z;
  232.    }
  233. }
  234.  
  235.  
  236.  
  237. static void write_hit_record( GLcontext *ctx )
  238. {
  239.    GLuint i;
  240.    GLuint zmin, zmax, zscale = (~0u);
  241.  
  242.    /* HitMinZ and HitMaxZ are in [0,1].  Multiply these values by */
  243.    /* 2^32-1 and round to nearest unsigned integer. */
  244.  
  245.    assert( ctx != NULL ); /* this line magically fixes a SunOS 5.x/gcc bug */
  246.    zmin = (GLuint) ((GLfloat) zscale * ctx->Select.HitMinZ);
  247.    zmax = (GLuint) ((GLfloat) zscale * ctx->Select.HitMaxZ);
  248.  
  249.    WRITE_RECORD( ctx, ctx->Select.NameStackDepth );
  250.    WRITE_RECORD( ctx, zmin );
  251.    WRITE_RECORD( ctx, zmax );
  252.    for (i=0;i<ctx->Select.NameStackDepth;i++) {
  253.       WRITE_RECORD( ctx, ctx->Select.NameStack[i] );
  254.    }
  255.  
  256.    ctx->Select.Hits++;
  257.    ctx->Select.HitFlag = GL_FALSE;
  258.    ctx->Select.HitMinZ = 1.0;
  259.    ctx->Select.HitMaxZ = -1.0;
  260. }
  261.  
  262.  
  263.  
  264. void gl_LoadName( GLcontext *ctx, GLuint name )
  265. {
  266.    if (INSIDE_BEGIN_END(ctx)) {
  267.       gl_error( ctx, GL_INVALID_OPERATION, "glLoadName" );
  268.       return;
  269.    }
  270.    if (ctx->RenderMode!=GL_SELECT) {
  271.       return;
  272.    }
  273.    if (ctx->Select.NameStackDepth==0) {
  274.       gl_error( ctx, GL_INVALID_OPERATION, "glLoadName" );
  275.       return;
  276.    }
  277.    if (ctx->Select.HitFlag) {
  278.       write_hit_record( ctx );
  279.    }
  280.    if (ctx->Select.NameStackDepth<MAX_NAME_STACK_DEPTH) {
  281.       ctx->Select.NameStack[ctx->Select.NameStackDepth-1] = name;
  282.    }
  283.    else {
  284.       ctx->Select.NameStack[MAX_NAME_STACK_DEPTH-1] = name;
  285.    }
  286. }
  287.  
  288.  
  289. void gl_PushName( GLcontext *ctx, GLuint name )
  290. {
  291.    if (INSIDE_BEGIN_END(ctx)) {
  292.       gl_error( ctx, GL_INVALID_OPERATION, "glPushName" );
  293.       return;
  294.    }
  295.    if (ctx->RenderMode!=GL_SELECT) {
  296.       return;
  297.    }
  298.    if (ctx->Select.HitFlag) {
  299.       write_hit_record( ctx );
  300.    }
  301.    if (ctx->Select.NameStackDepth<MAX_NAME_STACK_DEPTH) {
  302.       ctx->Select.NameStack[ctx->Select.NameStackDepth++] = name;
  303.    }
  304.    else {
  305.       gl_error( ctx, GL_STACK_OVERFLOW, "glPushName" );
  306.    }
  307. }
  308.  
  309.  
  310.  
  311. void gl_PopName( GLcontext *ctx )
  312. {
  313.    if (INSIDE_BEGIN_END(ctx)) {
  314.       gl_error( ctx, GL_INVALID_OPERATION, "glPopName" );
  315.       return;
  316.    }
  317.    if (ctx->RenderMode!=GL_SELECT) {
  318.       return;
  319.    }
  320.    if (ctx->Select.HitFlag) {
  321.       write_hit_record( ctx );
  322.    }
  323.    if (ctx->Select.NameStackDepth>0) {
  324.       ctx->Select.NameStackDepth--;
  325.    }
  326.    else {
  327.       gl_error( ctx, GL_STACK_UNDERFLOW, "glPopName" );
  328.    }
  329. }
  330.  
  331.  
  332.  
  333. /**********************************************************************/
  334. /*                           Render Mode                              */
  335. /**********************************************************************/
  336.  
  337.  
  338.  
  339. /*
  340.  * NOTE: this function can't be put in a display list.
  341.  */
  342. GLint gl_RenderMode( GLcontext *ctx, GLenum mode )
  343. {
  344.    GLint result;
  345.  
  346.    if (INSIDE_BEGIN_END(ctx)) {
  347.       gl_error( ctx, GL_INVALID_OPERATION, "glRenderMode" );
  348.    }
  349.  
  350.    switch (ctx->RenderMode) {
  351.       case GL_RENDER:
  352.      result = 0;
  353.      break;
  354.       case GL_SELECT:
  355.      if (ctx->Select.HitFlag) {
  356.         write_hit_record( ctx );
  357.      }
  358.      if (ctx->Select.BufferCount > ctx->Select.BufferSize) {
  359.         /* overflow */
  360. #ifdef DEBUG
  361.             gl_warning(ctx, "Feedback buffer overflow");
  362. #endif
  363.         result = -1;
  364.      }
  365.      else {
  366.         result = ctx->Select.Hits;
  367.      }
  368.      ctx->Select.BufferCount = 0;
  369.      ctx->Select.Hits = 0;
  370.      ctx->Select.NameStackDepth = 0;
  371.      break;
  372.       case GL_FEEDBACK:
  373.      if (ctx->Feedback.Count > ctx->Feedback.BufferSize) {
  374.         /* overflow */
  375.         result = -1;
  376.      }
  377.      else {
  378.         result = ctx->Feedback.Count;
  379.      }
  380.      ctx->Feedback.Count = 0;
  381.      break;
  382.       default:
  383.      gl_error( ctx, GL_INVALID_ENUM, "glRenderMode" );
  384.      return 0;
  385.    }
  386.  
  387.    switch (mode) {
  388.       case GL_RENDER:
  389.          break;
  390.       case GL_SELECT:
  391.      if (ctx->Select.BufferSize==0) {
  392.         /* haven't called glSelectBuffer yet */
  393.         gl_error( ctx, GL_INVALID_OPERATION, "glRenderMode" );
  394.      }
  395.      break;
  396.       case GL_FEEDBACK:
  397.      if (ctx->Feedback.BufferSize==0) {
  398.         /* haven't called glFeedbackBuffer yet */
  399.         gl_error( ctx, GL_INVALID_OPERATION, "glRenderMode" );
  400.      }
  401.      break;
  402.       default:
  403.      gl_error( ctx, GL_INVALID_ENUM, "glRenderMode" );
  404.      return 0;
  405.    }
  406.  
  407.    ctx->RenderMode = mode;
  408.    ctx->NewState |= NEW_ALL;
  409.  
  410.    return result;
  411. }
  412.  
  413.